<?php

namespace App\Admin\Controllers;

use App\Models\NoticeTemplate;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class NoticeTemplateController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '配置模版';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new NoticeTemplate);
        $user_id =Admin::user()->id;
        if($user_id!=1){
            $grid->model()->where(['admin_user_id'=>$user_id]);
        }
        $grid->column('notice_template_id', __('Notice template id'));
        $grid->column('frequency', __('频率'));
        $grid->column('template_name', __('模版名称'));
        $grid->column('email', __('Email'));
        $grid->column('phone', __('Phone'));
        $grid->column('created_at', __('Created at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(NoticeTemplate::findOrFail($id));

        $show->field('notice_template_id', __('Notice template id'));
        $show->field('frequency', __('Frequency'));
        $show->field('email', __('Email'));
        $show->field('phone', __('Phone'));
        $show->field('greater_num_stop', __('Greater num stop'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));
        $show->field('created_at', __('Created at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new NoticeTemplate);
        $form->text('template_name','模版名称');
        $form->number('frequency', __('频率'))->help('频率单位为分钟')->default(1);
        $form->email('email', __('邮箱'));
        $form->mobile('phone', __('手机'));
        $form->number('greater_num_stop', __('捕获异常次数'))->help('异常几次之后报告异常')->default(1)->required();
        $form->hidden('admin_user_id','')->default(Admin::user()->id);
        return $form;
    }
}
